$(function () {
    //======================
    //======设置查询条件========
    //======================
    //内部公司
    $("#cwflsfc_toolbar [name='gsId']").gsjgtree(1);
    //计划年份
    $("#cwflsfc_toolbar [name='year']").numberspinner({
        min: 2000,
        max: 2100,
        editable: true
    });
    //计划月份
    $("#cwflsfc_toolbar [name='month']").numberspinner({
        min: 1,
        max: 12,
        editable: true
    });
    //月末结转
    $("#cwflsfc_toolbar [name='ymjz']").click(function () {
        var year = $("#cwflsfc_toolbar [name='year']").val();
        var month = $("#cwflsfc_toolbar [name='month']").val();
        $.messager.confirm('提示', '是否确认对' + year + '年' + month + '月财务库存分类收发存数据进行月末结转?', function (r) {
            if (r) {
                $.ajax({
                    url: $.webPath() + "/report/cwflsfc/ymjz",
                    dataType: 'json',
                    type: 'POST',
                    data: {
                        year: $("#cwflsfc_toolbar [name='year']").val(),
                        month: $("#cwflsfc_toolbar [name='month']").val()
                    },
                    beforeSend: function () {
                        $.mask();
                    },
                    success: function (data) {

                        $.mask.hide();
                        if (data.statusCode == 200) {
                            $.messager.show({
                                title: '提示',
                                msg: 3000,
                                showType: 'slide'
                            });
                        } else {
                            $.messager.alert('提示', data.message, 'error');
                        }
                    },
                    error: function () {
                    }
                });
            }
        });

    });
    //搜索按钮
    $("#cwflsfc_toolbar [name='searchButton']").click(function () {
        $(grid).datagrid({
            queryParams: {
                year: $("#cwflsfc_toolbar [name='year']").val(),
                month: $("#cwflsfc_toolbar [name='month']").val(),
                gsId: $("#cwflsfc_toolbar [name='gsId']").val()
            }});
    });


//======================
//========统计列表========
//======================
    var columns = [
        [
            {field: 'gsjc', title: '公司', rowspan: 2, width: 150,align: 'center'},
            {field: 'kmmc', title: '库存商品', rowspan: 2, width: 150,align: 'center'},
            {field: 'qc', title: '期初', rowspan: 2, width: 100,align: 'right'},
            {title: '本期购入', colspan: 5, width: 100,align: 'right'},
            {title: '本期发出', colspan: 2, width: 100,align: 'right'},
            {field: 'qm', title: '期末', rowspan: 2, width: 100,align: 'right'}
        ],
        [
            {field: 'cgjxfp', title: '采购结算', width: 100, formatter: function (v, r, i) {
                if (v && r.kmmc != '合计') {
                    return '<a href="javascript:void(0)" style="color:#0000ff;" class="cgjs" gsid="' + r.gsid + '" codeid="'+ r.codeid+'">' + formatSl2(v,",",".") + '</a>';
                } else {
                    return formatSl2(v,",",".");
                }
            },align: 'right'},
            {field: 'wljxfp', title: '物流结算', width: 100, formatter: function (v, r, i) {
                if (v && r.kmmc != '合计') {
                    return '<a href="javascript:void(0)" style="color:#0000ff;" class="wljs" gsid="' + r.gsid + '" codeid="'+ r.codeid+'">' + formatSl2(v,",",".") + '</a>';
                } else {
                    return formatSl2(v,",",".");
                }
            },align: 'right'},
            {field: 'cgjxgs', title: '采购估收', width: 100, formatter: function (v, r, i) {
                if (v && r.kmmc != '合计') {
                    return '<a href="javascript:void(0)" style="color:#0000ff;" class="cggs" gsid="' + r.gsid + '" codeid="'+ r.codeid+'">' + formatSl2(v,",",".") + '</a>';
                } else {
                    return formatSl2(v,",",".");
                }
            },align: 'right'},
            {field: 'wljxgs', title: '物流估收', width: 100, formatter: function (v, r, i) {
                if (v && r.kmmc != '合计') {
                    return '<a href="javascript:void(0)" style="color:#0000ff;" class="wlgs" gsid="' + r.gsid + '" codeid="'+ r.codeid+'">' + formatSl2(v,",",".") + '</a>';
                } else {
                    return formatSl2(v,",",".");
                }
            },align: 'right'},
            {field: 'cgje', title: '上期冲销', width: 100,align: 'right'},
            {field: 'xsfp', title: '销售结算', width: 100, formatter: function (v, r, i) {
                if (v && r.kmmc != '合计') {
                    return '<a href="javascript:void(0)" style="color:#0000ff;" class="xsjs" gsid="' + r.gsid + '" codeid="'+ r.codeid+'">' + formatSl2(v,",",".") + '</a>';
                } else {
                    return formatSl2(v,",",".");
                }
            },align: 'right'},
            {field: 'nbll', title: '内部领料', width: 100, formatter: function (v, r, i) {
                if (v && r.kmmc != '合计') {
                    return '<a href="javascript:void(0)" style="color:#0000ff;" class="nbll" gsid="' + r.gsid + '" codeid="'+ r.codeid+'">' + formatSl2(v,",",".") + '</a>';
                } else {
                    return formatSl2(v,",",".");
                }
            },align: 'right'}
        ]
    ];

    var grid = $('#cwflsfc_grid').datagrid({
        url: $.webPath() + '/report/cwflsfc/cwflsfcList',
        toolbar: "#cwflsfc_toolbar",
        rownumbers: true,
        singleSelect: true,
        collapsible: true,
        pageNumber: 1,
        fit: true,
        fitColumns: true,
        showFooter: true,
        queryParams: {
            year: $("#cwflsfc_toolbar [name='year']").val(),
            month: $("#cwflsfc_toolbar [name='month']").val()
        },
        onLoadSuccess: function () {
            $(".cgjs").unbind("click").bind("click", function () {
                showDetail('采购单结算明细', $.webPath() + "/report/cwsfc/yjscgdtj", $(this).attr("gsid"),$(this).attr("codeid"));
            });
            $(".cggs").unbind("click").bind("click", function () {
                showDetail('采购估收明细', $.webPath() + "/report/cwsfc/cggs", $(this).attr("gsid"),$(this).attr("codeid"));
            });
            $(".wljs").unbind("click").bind("click", function () {
                showDetail('物流结算明细', $.webPath() + "/report/cwsfc/wljs", $(this).attr("gsid"),$(this).attr("codeid"));
            });
            $(".wlgs").unbind("click").bind("click", function () {
                showDetail('物流估收明细', $.webPath() + "/report/cwsfc/wlgs", $(this).attr("gsid"),$(this).attr("codeid"));
            });
            $(".xsjs").unbind("click").bind("click", function () {
                showDetail('销售结算明细', $.webPath() + "/report/cwsfc/xsjs", $(this).attr("gsid"),$(this).attr("codeid"));
            });
            $(".nbll").unbind("click").bind("click", function () {
                showDetail('领料明细', $.webPath() + "/report/cwsfc/nbll", $(this).attr("gsid"),$(this).attr("codeid"));
            });
        },
        columns: columns
    });
//===========================================
//==============此页面用到的函数===================
//===========================================

    var showDetail = function (title, url, gsid,codeid) {
        var win = $.window({
            title: title,
            width: 1050,
            height: 470,
            cache: false,
            iconCls: 'icon-tip',
            url: url + "?gsId=" + gsid + "&codeId=" + codeid,
            modal: true,
            buttons: []
        });
    }
});
